<template>
  <uploader
    class="styled-uploader"
    action="http://114.55.1.185:3000/api/utile/uploadOSS"
    :beforeUpload="beforeUpload"
    :onSuccess="handlerSuccess"
    :showUploadList="showuploadList"
    v-bind="props"
  >
    <slot name="content">
      <div class="uploader-container" :class="{ 'upload-width': uploadClass }">
        <FileImageOutlined />
        <h4>{{ text }}</h4>
      </div>
    </slot>

    <template #loading>
      <div class="uploader-container" :class="{ 'upload-width': uploadClass }">
        <LoadingOutlined spin />
        <h4>上传中</h4>
      </div>
    </template>
    <template #uploaded>
      <div class="uploader-container" :class="{ 'upload-width': uploadClass }">
        <img :src="showUploadedUrl" v-if="showUploaded" />
        <template v-else>
          <FileImageOutlined />
          <h4>{{ text }}</h4>
        </template>
      </div>
    </template>
  </uploader>
</template>

<script lang="ts" setup>
import { message } from 'ant-design-vue'
import uploader from './Uploader.vue'
import { FileImageOutlined, LoadingOutlined } from '@ant-design/icons-vue'
const props = defineProps({
  text: {
    type: String,
    default: '上传图片',
  },
  showUploaded: {
    type: Boolean,
    default: false,
  },
  showUploadedUrl: {
    type: String,
    default: '',
  },
  fileType: {
    type: Array,
    default: () => ['image/png', 'image/jpeg', 'image/jpg'],
  },
  fileSize: {
    type: Number,
    default: 5,
  },
  fileTypeErrorMessage: {
    type: String,
    default: '文件上传类型不正确',
  },
  fileSizeErrorMessage: {
    type: String,
    default: '文件大小不能超过5M',
  },
  fileMessage: {
    type: String,
    default: '文件不存在',
  },
  showuploadList: {
    type: Boolean,
    default: true,
  },
  uploadClass: {
    type: Boolean,
    default: false,
  },
})
const emits = defineEmits(['success'])
const beforeUpload = (file: File) => {
  if (file) {
    const { type, size } = file
    if (!props.fileType.includes(type)) {
      message.error(props.fileTypeErrorMessage)
      return false
    }
    if (size / 1024 / 1024 > props.fileSize) {
      message.error(props.fileSizeErrorMessage)
      return false
    }
  } else {
    message.error(props.fileMessage)
    return false
  }
}
const handlerSuccess = (res: any, file: File, fileList: any) => {
  emits('success', { res, file, fileList })
}
</script>

<style lang="scss" scoped>
.styled-uploader {
  .uploader-container {
    width: 100%;
    padding: 10px;
    color: #ffffff;
    background: #1890ff;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 5px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  }
  .uploader-container:hover {
    background: #40a9ff;
  }
  .uploader-container h4 {
    color: #ffffff;
    margin-bottom: 0;
    margin-left: 10px;
  }
  .uploader-container img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .upload-width {
    width: 110px;
    height: 32px;
  }
}
</style>
